package com.whz.gateway.common.util;

import lombok.experimental.UtilityClass;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.web.server.ServerWebExchange;

import java.net.URI;

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;

/**
 * exchange中的attribute辅助查询类
 *
 * @author whz
 * @see ServerWebExchangeUtils
 */
@UtilityClass
public class AttributeUtils {

    /**
     * 获取strip转换后的route
     *
     * @param exchange exchange
     * @return 待转发的route
     */
    public static Route getRouterAfterStrip(final ServerWebExchange exchange) {
        return exchange.getAttribute(GATEWAY_ROUTE_ATTR);
    }

    /**
     * 获取转换后的uri
     *
     * @param exchange exchange
     * @return 待转发的uri
     */
    public static URI getUrlAfterStrip(final ServerWebExchange exchange) {
        return exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
    }
}